<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Main extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('parser');
        $this->load->model('android');
    }

    function index() {
        $data = array(
            'title' => 'Welcome to Kitchen',
            'alert' => null
        );
        $this->parser->parse('theme/head', $data);
        $this->parser->parse('theme/login', $data);
        $this->load->view('theme/foot');
    }

    function auth() {
        $this->form_validation->set_rules('name', 'Username', 'required');
        $this->form_validation->set_rules('pass', 'Password', 'required');
        if ($this->form_validation->run() == FALSE) {
            redirect('main');
        } else {
            $log = array(
                'username' => $this->input->post('name'),
                'password' => $this->encrypt->sha1($this->input->post('pass'))
            );
            $login = $this->android->login($log); //kirim ke database
            if ($login->privilege_id == 1) {
                $ses = array(
                    'username' => $login->username,
                    'login' => TRUE,
                    'privilege' => 1
                );
                $this->session->set_userdata($ses);
                redirect('admin');
            } elseif ($login->privilege_id == 2) {
                $ses = array(
                    'username' => $login->username,
                    'login' => TRUE,
                    'privilege' => 2
                );
                $this->session->set_userdata($ses);
                redirect('member');
            } else {
                redirect();
            }
        }
    }

    function register() {
        $this->form_validation->set_rules('name', 'Username', 'required');
        $this->form_validation->set_rules('pass', 'Password', 'required');
        if ($this->form_validation->run() == FALSE) {
            redirect();
        } else {
            $username = $this->input->post('name');
            $result = $this->android->cekUsername($username);
            if ($result > 0) {
                redirect('main/usernameAlready');
            } else {
                $kirim = array(
                    'username' => $this->input->post('name'),
                    'password' => $this->encrypt->sha1($this->input->post('pass')),
                    'privilege_id' => 2
                );
                $this->android->register($kirim); //kirim ke database
//                redirect('main/registerSuccess');
                $this->auth();
            }
        }
    }

    function registerSuccess() {
        $data['alert'] = '<div class="alert alert-info"><strong>Register Success</strong></div>';
        $data['title'] = 'Register Success';
        $this->parser->parse('themes/alert', $data);
    }

    function usernameAlready() {
        $data['alert'] = '<div class="alert alert-error"><strong>Username sudah digunakan</strong></div>';
        $data['title'] = 'Username already exist';
        $this->parser->parse('theme/head', $data);
        $this->parser->parse('theme/alert', $data);
        $this->load->view('theme/foot');
    }

    function logout() {
        $this->session->sess_destroy();
        redirect();
    }

    function notfound() {
        $this->load->view('notfound');
    }

}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>
